package com.example.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.response.Result;
import com.example.user.entity.CartItem;
import com.example.user.feign.ProductFeignClient;
import com.example.user.mapper.CartItemMapper;
import com.example.user.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartItemMapper cartItemMapper;

    @Autowired
    private ProductFeignClient productFeignClient;

    @Override
    public Result<Map<CartItem, Object>> getCartItemsWithProducts(Long userId) {
        // 1. 获取用户的购物车项
        List<CartItem> cartItems = getCartItems(userId).getData();
        
        // 2. 查询每个购物车项对应的商品信息
        Map<CartItem, Object> cartItemProducts = new HashMap<>();
        for (CartItem cartItem : cartItems) {
            Result<Object> productResult = productFeignClient.getProduct(cartItem.getProductId());
            cartItemProducts.put(cartItem, productResult.getData());
        }
        
        return Result.success(cartItemProducts);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> addToCart(Long userId, Long productId, Integer quantity) {
        CartItem cartItem = new CartItem();
        cartItem.setUserId(userId);
        cartItem.setProductId(productId);
        cartItem.setQuantity(quantity);
        cartItem.setCreateTime(LocalDateTime.now());
        cartItem.setUpdateTime(LocalDateTime.now());
        
        return Result.success(cartItemMapper.insert(cartItem) > 0);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> updateCartItem(Long userId, Long productId, Integer quantity) {
        CartItem cartItem = cartItemMapper.selectOne(
            new LambdaQueryWrapper<CartItem>()
                .eq(CartItem::getUserId, userId)
                .eq(CartItem::getProductId, productId)
        );
        
        if (cartItem != null) {
            cartItem.setQuantity(quantity);
            cartItem.setUpdateTime(LocalDateTime.now());
            return Result.success(cartItemMapper.updateById(cartItem) > 0);
        }
        
        return Result.error("购物车项不存在");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> removeFromCart(Long userId, Long productId) {
        return Result.success(cartItemMapper.delete(
            new LambdaQueryWrapper<CartItem>()
                .eq(CartItem::getUserId, userId)
                .eq(CartItem::getProductId, productId)
        ) > 0);
    }

    @Override
    public Result<List<CartItem>> getCartItems(Long userId) {
        List<CartItem> cartItems = cartItemMapper.selectList(
            new LambdaQueryWrapper<CartItem>()
                .eq(CartItem::getUserId, userId)
        );
        return Result.success(cartItems);
    }
} 